<?php

namespace App\Http\Controllers;

use App\Att;
use App\Bid;
use App\Pro;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

class ProController extends Controller
{
    // 获取借款页面
    public function jie(){
        return view('woyaojiekuan');
    }
    // 借款逻辑
    public function jiePost(){
        $user = Auth::user();
        // 开启事务
        DB::beginTransaction();
        try{
            $pro = Pro::create([
                'uid'=>$user->uid,
                'name'=>$user->name,
                'money'=>request('money')*100,
                'mobile'=>request('mobile'),
                'pubtime'=>time()
            ]);
        }catch(\Exception $e){
            DB::rollback();
            throw $e;
        }
        try{
            $att = Att::create([
                'pid'=>$pro->pid,
                'uid'=>$user->uid,
                'age'=>request('age')
            ]);
        }catch(\Exception $e){
            DB::rollback();
            throw $e;
        }
        // 提交事务
        DB::commit();
        return "申请成功";
    }
    public function touzi($pid){
        $pro = Pro::find($pid);
        return view('lijitouzi',compact('pro'));
    }
    public function touziPost(Request $req,$pid){
        $user = $req->user();
        $bid = new Bid();
        $pro = Pro::where('pid',$pid)->first();
        $bid->uid = $user->uid;
        $bid->pid = $pid;
        $bid->title = $pro->title;
        $bid->money = $req->money * 100;
        $bid->pubtime = time();
        $bid->save();
        $pro->recive += $bid->money;
        $pro->save();
        if($pro->recive == $pro->money){
            $this->touziDone($pid);
        }
        return "购买成功";
    }
    public function touziDone($pid){
        // 修改项目状态
        $pro = Pro::find($pid);
        $pro->status = 2;
        $pro->save();
        // 给借款人生成还款账单
        // 本金+利息 等息
        $amount = $pro->money / $pro->hrange + ($pro->money * $pro ->rate) / 1200;
        $data = ['uid'=>$pro->uid,'pid'=>$pid,'title'=>$pro->title];
        $data['amount'] = $amount;
        for($i = 1;$i <= $pro->hrange;$i++){
            $paydate = date('Ymd',strtotime("+ $i months"));
            $data['paydate'] = $paydate;
            DB::table('hks')->insert($data);
        }
        // 为投资人生成预收益账单
        $bid = \App\Bid::where('pid',$pid)->get();
        $row = ['pid'=>$pid,'title'=>$pro->title];
        $enddate = date("Ymd",strtotime("+ $pro->hrange months"));
        $row['enddate'] = $enddate;
        foreach($bid as $b){
            $row['uid'] = $b->uid;
            $row['amount'] = ($b->money * $pro->rate)/36500;
            DB::table('tasks')->insert($row);
        }
    }
    public function myzd(){
        $hks = DB::table('hks')->where('uid',Auth::Id())->get();
        return view('myzd',compact('hks'));
    }
    public function mytz(){
        $bid = Bid::where('bids.uid',Auth::Id())->whereIn('status',[1,2])->join('projects','bids.pid','=','projects.pid')->get(['bids.*','projects.status']);
        return view('mytz',compact('bid'));
    }
    public function mysy(){
        $grows = DB::table('grows')->where('uid',Auth::Id())->get();
        return view('mysy',compact('grows'));
    }
}
